<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <h1 style="margin: 0">《{{ courseName }}》课程名单（成绩录入）</h1>
    </el-card>

    <!-- 筛选 -->
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="学号" prop="schoolNumber">
          <el-input
            v-model="queryParams.schoolNumber"
            placeholder="请输入学号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>

      <el-table v-loading="loading" :data="studentList">
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column label="性别" align="center" prop="sex">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_user_sex"
              :value="scope.row.sex"
            />
          </template>
        </el-table-column>
        <el-table-column label="学号" align="center" prop="schoolNumber" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="年龄" align="center" prop="age" />
        <el-table-column label="年级" align="center" prop="grade">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.ocss_grade"
              :value="scope.row.grade"
            />
          </template>
        </el-table-column>
        <el-table-column label="成绩" align="center" prop="achievement">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.achievement == null">
              未录入成绩
            </el-tag>
            <el-tag type="success" v-else> {{ scope.row.achievement }} </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-popover placement="top" width="160" trigger="hover">
              <el-input
                v-model="achievementValue"
                placeholder="请输入成绩"
                style="margin-bottom: 10px"
              />
              <div style="text-align: right; margin: 0">
                <el-button
                  type="primary"
                  size="mini"
                  @click="enterScope(scope.row)"
                  >确定</el-button
                >
              </div>
              <el-button
                slot="reference"
                size="mini"
                type="text"
                icon="el-icon-edit"
                >{{
                  scope.row.achievement !== null ? "修改成绩" : "录入成绩"
                }}</el-button
              >
            </el-popover>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { courseStudentList, addAchievement } from "@/api/system/achievement";

export default {
  name: "CourseDetail",
  dicts: ["sys_user_sex", "ocss_grade"],
  data() {
    return {
      // 总条数
      total: 0,
      // 学生信息表格数据
      studentList: [],
      // 显示搜索条件
      showSearch: true,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolNumber: null,
        name: null,
      },
      courseId: 0,
      courseName: "",
      achievementValue: "",
    };
  },
  created() {
    this.courseId = this.$route.params.id;
    this.courseName = this.$route.params.name;
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      courseStudentList(this.queryParams, this.courseId).then((response) => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 录入成绩
    enterScope(row) {
      if (this.achievementValue.trim().length !== 0) {
        addAchievement({
          courseId: this.courseId,
          studentId: row.id,
          achievement: this.achievementValue,
        }).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getList();
          }
        });
      }
    },
  },
};
</script>
